<?php
use think\Config;
use think\Session;

//获取请求控制器
function getRequestUrl(){
	$request = \think\Request::instance();
	$module = $request->module() ;
	$controller = lcfirst($request->controller());
	$action = $request->action();
	$requestParam = $request->param();

	$controller = preg_replace_callback('/([A-Z]{1})/',function($matches){
    return '_'.strtolower($matches[0]);
  },$controller);

	return [
		'controller' => $controller,
		'action' => $action,
		'param' => $requestParam,
		'is_post' => $request->isPost(),
		'url' => "/{$module}/{$controller}/{$action}",
		'wild' => "/{$module}/{$controller}/*",
	];
}


//字符串不存在
function checkString(&$value){
	return isset($value) ? $value:"";
}

//字符串不存在
function checkInt(&$value){
	return isset($value) ? (int)$value: 0;
}


function conversion($key, $conversion){
	return isset($conversion[$key]) ? $conversion[$key]: '';
}


function export($data, $header = []){
	$length = isset($data[0])? count($data[0]) : 0;
	$name = 12;
	error_reporting(E_ALL);
	$objPHPExcel = new PHPExcel();
	//验证title
	if(is_array($header)&&!empty($header)){
		$exportData = [];
		$column = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','x','y','Z'];
		$column_i = 0;
		foreach ($header as $key => $vo) {
			// dump( $vo );die();
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue((string)($column[$column_i]."1"), $vo['title']); 
			$column_i++;
		}

		$num = 2;
		foreach ($data as $key => $vo) {
			$column_i = 0;
			extract($vo);
			foreach ($header as $key => $field) {
				if(!empty($field['value'])){
					eval( '$value = "'.$field['value'].'";' );
					$objPHPExcel->setActiveSheetIndex(0)->setCellValue((string)($column[$column_i].$num), $value); 
				}else{
					$objPHPExcel->setActiveSheetIndex(0)->setCellValue((string)($column[$column_i].$num), $$field['field']); 
				}
				
				$column_i ++;
			}
			$num ++;
		}
	}
	// $objPHPExcel->getActiveSheet()->setTitle('User');
	$objPHPExcel->setActiveSheetIndex(0);
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment;filename="'.$name.'.xls"');
	header('Cache-Control: max-age=0');
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');
	exit;
}